package com.uchengwang.ui.cm_ui.cycle.ui;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.view.WindowManager;
import android.webkit.WebChromeClient;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationListener;
import com.uchengwang.R;
import com.uchengwang.functions.http.config.BaseConfig;
import com.uchengwang.functions.http.listener.OnResultListener;
import com.uchengwang.ui.UChengApplication;
import com.uchengwang.ui.base_ui.ui.AllBaseActivity;
import com.uchengwang.ui.cm_ui.cycle.bean.H5MapLocationBean;
import com.uchengwang.ui.cm_ui.cycle.bridge.WebViewJsHandler;
import com.uchengwang.ui.listener.PermissionsListener;
import com.uchengwang.ui.self.jsbridge.BridgeWebView;
import com.uchengwang.ui.self.jsbridge.CallBackFunction;
import com.uchengwang.ui.self.jsbridge.DefaultHandler;
import com.uchengwang.ui.utils.JsonUtils;
import com.uchengwang.ui.utils.MapManager;
import com.uchengwang.ui.utils.PermissionUtils;
import com.uchengwang.ui.utils.UCommonUtil;

import de.greenrobot.event.EventBus;

/**
 * Created by U城网 on 2018/5/11.
 * 加载h5界面  整个h5界面圈填充
 */

public class LoadWebHtmlFillActivity extends AllBaseActivity implements View.OnClickListener{

    private BridgeWebView bridgeWebView;
    private FrameLayout load_fl;
    private String url;//网址和网址标题
    private int type;//1社圈广场
    private int pubType;//1关注圈 2会场圈

    private WebViewJsHandler handler;

    private RelativeLayout load_bottom;
    private EditText bottom_edit;
    private Button bottom_bt;
    private CallBackFunction function;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_load_cycle_web);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

        url = getIntent().getStringExtra(BaseConfig.URL);
        type = getIntent().getIntExtra(BaseConfig.TYPE, -1);
        pubType = getIntent().getIntExtra(BaseConfig.PUB_TYPE, 0);
        Log.d("jjf", "加载的url=="+url);

        EventBus.getDefault().register(this);


        initView();
    }

    private void initView() {
        load_fl = (FrameLayout) findViewById(R.id.load_fl);
        bridgeWebView = new BridgeWebView(this);

        load_bottom = (RelativeLayout) findViewById(R.id.load_bottom);
        load_bottom.setOnClickListener(this);
        bottom_edit = (EditText) findViewById(R.id.bottom_edit);
        bottom_bt = (Button) findViewById(R.id.bottom_bt);
        bottom_bt.setOnClickListener(this);

        load_fl.addView(bridgeWebView);
        bridgeWebView.setDefaultHandler(new DefaultHandler());
        bridgeWebView.setWebChromeClient(new WebChromeClient());
        /**
         * js发送给按住消息   submitFromWeb 是js调用的方法名    安卓\返回给js
         */

        handler = WebViewJsHandler.getHandler();
        handler.registerWebviewJsHandlers(this, bridgeWebView);

        System.out.println("打开的新界面:"+handler);

        bridgeWebView.loadUrl(url);
    }

    /**
     * 刷新数据
     * */
    public void onEvent(String refresh) {
        if (!TextUtils.isEmpty(refresh) && refresh.equals("刷新")){
            bridgeWebView.reload();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (BaseConfig.isRelease || UChengApplication.isCycleDelete){
            bridgeWebView.reload();
            BaseConfig.isRelease = false;
            UChengApplication.isCycleDelete = false;
        }

    }

    @Override
    public void onBackPressed() {
        switch (type){
            case 1://社圈广场
                setResult(1, new Intent());
                break;
        }
        if (bridgeWebView.canGoBack()){
            bridgeWebView.goBack();
        }else {
            super.onBackPressed();
        }


    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        handler.clearUrl();
        handler = null;

        if (bridgeWebView != null) {
            ViewParent parent = bridgeWebView.getParent();
            if (parent != null) {
                ((ViewGroup) parent).removeView(bridgeWebView);
            }
            bridgeWebView.removeAllViews();
            bridgeWebView.destroy();
            bridgeWebView = null;

            load_fl.removeAllViews();
            load_fl = null;
        }

        MapManager.getMapManager().desLocation();
        EventBus.getDefault().unregister(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.bottom_bt:
                String content = bottom_edit.getText().toString();
                if (!TextUtils.isEmpty(content)){
                    function.onCallBack(content);
                }
                bottom_edit.setText("");
                load_bottom.setVisibility(View.GONE);
                UCommonUtil.getUtil().hideSoftInput(this, bottom_edit);

                break;
            case R.id.load_bottom:
                load_bottom.setVisibility(View.GONE);
                UCommonUtil.getUtil().hideSoftInput(this, bottom_edit);
                break;
        }
    }

    //打开软键盘
    public void openEdit(CallBackFunction f, String data){
        if (load_bottom.getVisibility() ==View.GONE){
            function = f;
            load_bottom.setVisibility(View.VISIBLE);
            bottom_edit.requestFocus();
            if (!TextUtils.isEmpty(data)){
                bottom_edit.setHint(data);
            }else {
                bottom_edit.setHint("");
            }
            UCommonUtil.getUtil().showSoftInput(this, bottom_edit);
        }
    }

    //获取定位信息
    public void getLocation(final OnResultListener listener){
        final H5MapLocationBean locationBean = new H5MapLocationBean();
        PermissionUtils.getUtils().checkLocation(this, new PermissionsListener() {
            @Override
            public void onSuccess() {
                MapManager.getMapManager().startLocation(new AMapLocationListener() {
                    @Override
                    public void onLocationChanged(AMapLocation aMapLocation) {
                        Log.d("ucw", "定位="+aMapLocation.toString());

                        if (aMapLocation != null){
                            locationBean.setLatitude(aMapLocation.getLatitude());
                            locationBean.setLongitude(aMapLocation.getLongitude());
                            locationBean.setStatus(0);

                        }else {
                            locationBean.setStatus(1);
                            locationBean.setMsg("定位失败");
                        }

                        String json = JsonUtils.getManager().beanToString(locationBean);
                        listener.onResult(0, "", 0, json);
                    }
                });
            }

            @Override
            public void onFailed() {
                locationBean.setStatus(1);
                locationBean.setMsg("缺少定位权限");
                String json = JsonUtils.getManager().beanToString(locationBean);
                listener.onResult(0, "", 0, json);
                UCommonUtil.getUtil().showToast(LoadWebHtmlFillActivity.this, "缺少定位权限");
            }
        });
    }

    @Override
    protected void onStop() {
        super.onStop();
        MapManager.getMapManager().stopLocation();
    }
}
